<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/8/2
 * Time: 10:39
 */

namespace app\index\controller;
use think\Session;
class Weixinb{
    public function getWxAccessToken(){
        //1.请求url地址
        $appid = 'wxddd2363aff49a909';
        $appsecret =  'f9b63690e4c1acd765753d2ee99c46f3';
        $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$appsecret;
        //2初始化
        $ch = curl_init();
        //3.设置参数
        curl_setopt($ch , CURLOPT_URL, $url);
        curl_setopt($ch , CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        //4.调用接口
        $res = curl_exec($ch);
        //5.关闭curl
        curl_close( $ch );

//        if( curl_errno($ch) ){
//            var_dump( curl_error($ch) );
//        }

        $arr = json_decode($res, true);
        Session::set('access_token',$arr['access_token']);
        dump(Session::get('access_token'));
        dump($arr);
    }

    function createMenu(){//设置按钮链接
        dump(Session::get('access_token'));
        $data = '{
     "button":[
     {
          "type":"view",
          "name":"首页",
          "url":"http://118.193.221.147/ParallelToTheNet/public"
      },
      {
           "type":"view",
           "name":"登录",
           "url":"http://118.193.221.147/ParallelToTheNet/public/index/index/login"
      },
      {
           "name":"菜单",
           "sub_button":[
            {
               "type":"click",
               "name":"hello word",
               "key":"V1001_HELLO_WORLD"
            },
            {
               "type":"click",
               "name":"赞一下我们",
               "key":"V1001_GOOD"
            }]
       }]
}';
        $ACCESS_TOKEN = Session::get('access_token');
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".$ACCESS_TOKEN);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $tmpInfo = curl_exec($ch);
        if (curl_errno($ch)) {
            return curl_error($ch);
        }

        curl_close($ch);
        return $tmpInfo;


    }

//获取菜单
    function getMenu(){
        return file_get_contents("https://api.weixin.qq.com/cgi-bin/menu/get?access_token=".ACCESS_TOKEN);
    }

//删除菜单
    function deleteMenu(){
        return file_get_contents("https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=".ACCESS_TOKEN);
    }


}